package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;


    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).get();
    }

    @Override
    public List<StrategyComment> selectAll() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //创建Query对象,根据过滤条件查询总条数,此处没有限制条件,所以用空的查询对象查
        Query query = new Query();
        //过滤
        query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        //分页:按照时间倒序
        Pageable pageAble = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //返回给控制器
        Page<StrategyComment> page = DBHelper.query(template, StrategyComment.class, query, pageAble);
        return page;
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void deleteById(String id) {
        strategyCommentRepository.deleteById(id);
    }


    @Override
    public void saveOrUpdate(StrategyComment strategyComment, UserInfo userInfo) {

    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        //添加评论数据
        //设置创建时间
        strategyComment.setCreateTime(new Date());
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void commentThumb(String cid, String userId) {
        //根据评论的id查出评论的对象
        StrategyComment strategyComment = strategyCommentRepository.findById(cid).get();
        //拿取点赞列表
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断当前登录的id是否在此列表中
        if (thumbuplist.contains(userId)){
            //说明是取消点赞
            //移除
            thumbuplist.remove(userId);
            //点赞-1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
        }else {
            //说明是点赞
            thumbuplist.add(userId);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
        }
        //最后,将数据保存
        strategyCommentRepository.save(strategyComment);
    }
}
